package pageobjects;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import common.AbstractPage;
//import common.VisibilityOfElement;
import common.PageObjectCommonFunctions;
/**
 * This is the object class for the Questionnaire Page
 * Locators and functions related to that page are placed here
 *
 */

public class QuestionnairePage extends PageObjectCommonFunctions {

	/********************************************* LOCATORS *********************************************/
	private static final String HeaderCSS = "div[id='header']";
	private static final String AnswersCSS = "div[id='options'] li";
	private static final String AnswersButtonsCSS = "div[id='options'] li[class^='option'] button[type='submit']";
	private static final String QuestionHeaderCSS = "div[id='quiz'] > div[class='header'] > h2[class='question']";
	private static final String ProgressBarCSS = "div[id='progress'] span[style^='width']";											
	/***************************************************************************************************/
	public static final String PAGE_HEADER = "header";
	public static final String PROGRESS_BAR = "progressbar";


	Map<String, String> pageLocators = new HashMap<String, String>();
	private static final Log log = LogFactory.getLog(QuestionnairePage.class);

	/**
	 * Instantiate a map in the contructor so Locators 
	 * are easily available from the test scripts
	 * @param driver
	 */
	public QuestionnairePage(WebDriver driver) {
		
		super(driver);
		pageLocators.put(PAGE_HEADER, HeaderCSS);
		pageLocators.put(PROGRESS_BAR, ProgressBarCSS);

	}

	/**
	 * Get the particular header 
	 * (Might have to be moved a higher level)
	 * @param componentName
	 * @return
	 */
	public String getHeader(String componentName) {
		
		String header = driver.findElement(By.cssSelector(pageLocators.get(componentName))).getText();
		return header;
		
	}

	/**
	 * Returns the number of answers for the current question
	 * @return
	 */
	public int getNumberOfAvailableAnswers() {
		
		waitUntilOptionsAvailable(AnswersButtonsCSS);
		List<WebElement> availableAnswersList = driver.findElements(By.cssSelector(AnswersCSS));
		log.info("Number of available answers = " + availableAnswersList.size());
		int availableAnswers = availableAnswersList.size();
		return availableAnswers;
		
	}

	/**
	 * Its a health check that everything is in place correctly
	 * in terms of the page elements we expect
	 * @return
	 */
	public Boolean areAnswersClickackable() {
		
		log.info("Checking if all li elements contain buttons ");
		List<WebElement> availableButtonsList = driver.findElements(By.cssSelector(AnswersButtonsCSS));
		log.info("Number of buttons: " + availableButtonsList.size());
		if (availableButtonsList.size() == getNumberOfAvailableAnswers()) {
			return true;
		} else {
			return false;
		}
		
	}

	/**
	 * The main workflow method
	 * Clicks a random answer and
	 * @return
	 */
	public Boolean clickRandomAnswer() {
		
		int numberOfAvailableAnswers = getNumberOfAvailableAnswers();
		//Get a random answer (Can be a utility method later on)
		int answerNumber = (int) ((Math.random()) * (numberOfAvailableAnswers));
		
		if(numberOfAvailableAnswers > 0){
			log.info("User chose the answer number " + answerNumber);
			waitUntilOptionsAvailable(AnswersButtonsCSS);
			//Get a list of all elements and click to a random membe of the list
			driver.findElements(By.cssSelector("button[type='submit']")).get(answerNumber).click();	
			waitUntilOptionsAvailable(ProgressBarCSS);
			return false;
		}else{
			log.error("Something went wrong and there are no available answers");
			return true;
			}
			
		}

	/**
	 * Get a string with the current question
	 * @return
	 */
	public String getQuestion() {
		
		log.info("Current question is: " + driver.getTitle());
		String question = driver.getTitle();
		return question;

	}
	
	
	/**
	 * This is the wrapper for the wait condition 
	 * Avoid stale exceptions.The locator used is stable but suboptimal
	 * Need an expression which contains the actual element we are expecting to click
	 * waitUntilVisible(By.xpath("//div[@id='quiz']/form/div[@class='select-one']/ul/li["+loc+"]/button"));
	 */
	public void waitUntilOptionsAvailable(String locator) {
		
	    waitUntilVisible(By.cssSelector(locator));    
	  
	}
	
	

}
